home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / Digital Signatures / Digital Signature Demo / Source ƒ / CSignedDataFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-11  |  3.0 KB  |  134 lines  |  [TEXT/KAHL]

  1. /*
  2.  * CSignedDataFile.c
  3.  * Copyright © 1993 Apple Computer Inc.
  4.  * All Rights Reserved
  5.  *
  6.  * This subclass to CDataFile performs file-level
  7.  * Digital Signature processing according to the
  8.  * following model:
  9.  *
  10.  * If the file has a signature, it will be checked
  11.  * and a Failure condition raised if the signature
  12.  * is not verified. If the signature verifies correctly,
  13.  * the signer will be displayed in a Modal Dialog
  14.  * window.
  15.  *
  16.  * To sign a file, your application should execute
  17.  *    myFile->SignFileOnClose(TRUE) before closing
  18.  * the file. The signature will be appended when
  19.  * the file is closed.
  20.  *
  21.  * Note that the actual signature is not accessable
  22.  * to your application.
  23.  */
  24.  
  25. #include "CSignedDataFile.h"
  26. #include <Exceptions.h>
  27. #include <OCEErrors.h>
  28. #include <DigitalSignature.h>
  29.     
  30. void
  31. CSignedDataFile::ISignedDataFile(void)
  32. {
  33.         inherited::ISignature();
  34. }
  35.  
  36. /*
  37.  * SignFile is a soup-to-nuts signature handler. It signs
  38.  * a complete, closed, file, handling context creation
  39.  * if necessary. Note that it must be called as follows:
  40.  * 1. Get the FSSpec for this file.
  41.  * 2. Close the file (this deletes the CFile copy of
  42.  *    the FSSpec).
  43.  * 3. SignFile.
  44.  * 4. DisposeSignerContext()
  45.  */
  46. void
  47. CSignedDataFile::SignFile(
  48.         const FSSpec                *signerFile,
  49.         ConstStr255Param            promptString,
  50.         const FSSpec                *dataFile,
  51.         SIGStatusProcPtr            statusProc
  52.     )
  53. {
  54.         OSErr                    status;
  55.         Str255                    msg;
  56.         Size                    signatureSize;
  57.         
  58.         NewContext(kSIGSign);
  59.         signatureSize = SignPrepare(signerFile, promptString);
  60.         if (statusProc == gSIGStatusProc) {
  61.             GetIndString(msg, STRn_SIGStatusProc, kStatusSignString);
  62.             InitDefaultStatusProc(msg, dataFile->name);
  63.         }
  64.         status = SIGSignFile(
  65.                 gSIGContextPtr,
  66.                 signatureSize,
  67.                 dataFile,
  68.                 statusProc
  69.             );
  70.         DisposeDefaultStatusProc();
  71.         if (status != noErr)
  72.             DisposeSignerContext();
  73.         FailOSErr(status);
  74. }
  75.  
  76. /*
  77.  * VerifyFile is a soup-to-nuts signature handler. It verifies
  78.  * a named file. This must be called in the following sequence:
  79.  * 1. Get the file name.
  80.  * 2. Get the FSSpec associated with this file.
  81.  * 4. Call the VerifyFile method.
  82.  * 5. DisposeSignerContext.
  83.  * 6. If successful, open the file read-only (it was
  84.  *        set read-only when the file was signed).
  85.  */
  86. void
  87. CSignedDataFile::VerifyFile(
  88.         const FSSpec                *fileSpec,
  89.         SIGStatusProcPtr            statusProc
  90.     )
  91. {
  92.         OSErr                    status;
  93.         Str255                    msg;
  94.         
  95.         NewContext(kSIGVerify);
  96.         if (statusProc == gSIGStatusProc) {
  97.             GetIndString(msg, STRn_SIGStatusProc, kStatusVerifyString);
  98.             InitDefaultStatusProc(msg, fileSpec->name);
  99.         }
  100.         status = SIGVerifyFile(
  101.                 gSIGContextPtr,
  102.                 fileSpec,
  103.                 statusProc
  104.             );
  105.         DisposeDefaultStatusProc();
  106.         if (status != noErr
  107.          && status != kSIGInvalidCredentialErr)
  108.             DisposeSignerContext();
  109.         FailOSErr(status);
  110. }
  111.  
  112. Boolean
  113. CSignedDataFile::FileIsSigned(
  114.         const FSSpec                *fileSpec
  115.     )
  116. {
  117.         OSErr                    status;
  118.         Boolean                    result;
  119.         
  120.         status = SIGFileIsSigned(fileSpec);
  121.         switch (status) {
  122.         case noErr:
  123.             result = TRUE;
  124.             break;
  125.         case kSIGNoSignature:
  126.             result = FALSE;
  127.             break;
  128.         default:
  129.             FailOSErr(status);
  130.         }
  131.         return (result);
  132. }
  133.  
  134.